package jlaw.client;

import com.explodingpixels.macwidgets.*;
import com.explodingpixels.widgets.PopupMenuCustomizerUsingStrings;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import jlaw.*;

import com.explodingpixels.data.Rating;
import com.explodingpixels.macwidgets.plaf.ITunesTableUI;
import com.explodingpixels.widgets.TableUtils;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class PantallaPrincipal {
	
    private UnifiedToolBar fUnifiedToolBar;
	private JScrollPane taula;
    private SourceList fSourceList;
	
    public PantallaPrincipal(JFrame frame) {
    	System.out.println("principal window");
        fUnifiedToolBar = createUnifiedToolBar();
        fUnifiedToolBar.installWindowDraggerOnWindow(frame);
		
        JSplitPane splitPane = createSourceListAndMainArea();
		
        JScrollPane taula = crear();
        
        frame.add(fUnifiedToolBar.getComponent(), BorderLayout.NORTH);
        frame.add(splitPane, BorderLayout.CENTER);
        frame.add(taula, BorderLayout.EAST);
    }
    public JScrollPane crear()     	{

                Object[][] data = new String[][]{
                        {"All These Things I Hate (Revolve Around Me)", "Bullet For My Valentine", "The Poison"},
                        {"Cries In Vain", "Bullet For My Valentine", "The Poison"},
                        {"The End", "Bullet For My Valentine", "The Poison"},
                        {"Her Voice Resides", "Bullet For My Valentine", "The Poison"},
                        {"Hit The Floor", "Bullet For My Valentine", "The Poison"},
                        {"Intro", "Bullet For My Valentine Apocalyptica", "The Poison"},
                        {"The Poison", "Bullet For My Valentine", "The Poison"},
                        {"Room 409", "Bullet For My Valentine", "The Poison"},
                        {"Spit You Out", "Bullet For My Valentine", "The Poison"},
                        {"Suffocating Under Words Of Sorrow (What Can I Do)", "Bullet For My Valentine", "The Poison"},
                        {"Tears Don't Fall", "Bullet For My Valentine", "The Poison"},
                        {"4 Words (To Choke Upon)", "Bullet For My Valentine", "The Poison"},
                        {"10 Years Today", "Bullet For My Valentine", "The Poison"}
                };


                data = new Object[][]{
                        {Rating.FIVE_STARS, 4.0, "Cat"},
                        {Rating.TWO_STARS, 3.0, "Dog"},
                        {Rating.THREE_STARS, 8.0, "Parrot"},
                        {Rating.ONE_STAR, 5.0, "Goat"},
                        {Rating.FOUR_STARS, 2.0, "Tiger"},
                };

                JTable table = new JTable(new DefaultTableModel(data, new String[]{"Rating", "Value", "Animal"})) {
                    @Override
                    public Class<?> getColumnClass(int column) {
                        if (column == 0) {
                            return Rating.class;
                        } else if (column == 1) {
                            return Double.class;
                        } else {
                            return Object.class;
                        }
                    }
                };
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                table.setUI(new ITunesTableUI());

                table.getColumnModel().getColumn(0).setPreferredWidth(75);
                table.getColumnModel().getColumn(1).setPreferredWidth(50);
//funcio?
               // TableUtils.makeSortable(table, createDummySortDelegate());

                JScrollPane scrollPane = new JScrollPane(table);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
return scrollPane;
          //      JFrame frame = new JFrame();
           //     frame.add(scrollPane, BorderLayout.CENTER);
            //    frame.setSize(275, 125);
            //    frame.setLocationRelativeTo(null);
             //   frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              //  frame.setVisible(true);
            }

	

    private UnifiedToolBar createUnifiedToolBar() {
    	
    	System.out.println("imatge");
       // Icon blueGlobeIcon = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/DotMac.png"));
                    	System.out.println("imatge");

      //  Icon greyGlobeIcon = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/Network.png"));
       // Icon preferences = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/PreferencesGeneral.png"));
        //Icon gear = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/icons/Advanced.png"));

        AbstractButton greyGlobeButton =
                MacButtonFactory.makeUnifiedToolBarButton(
                        new JButton("un buto"));
        greyGlobeButton.setEnabled(true);

        AbstractButton blueButton = new JButton("un altre");
        blueButton.setEnabled(true);

        UnifiedToolBar toolBar = new UnifiedToolBar();
        toolBar.addComponentToLeft(MacButtonFactory.makeUnifiedToolBarButton(blueButton));
        toolBar.addComponentToLeft(greyGlobeButton);
        toolBar.addComponentToLeft(MacButtonFactory.makeUnifiedToolBarButton(new JButton("i un latre")));
        toolBar.addComponentToRight(MacButtonFactory.makeUnifiedToolBarButton(new JButton("i lultim")));

        return toolBar;
    }

    private JSplitPane createSourceListAndMainArea() {

       // Image blueGlobeImage = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/DotMac.png")).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
       // Image greyGlobeImage = new ImageIcon(PantallaPrincipal.class.getResource("/jlaw/icons/Network.png")).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);

      //  Icon blueGlobeIcon = new ImageIcon(blueGlobeImage);
       // Icon greyGlobeIcon = new ImageIcon(greyGlobeImage);

        SourceListCategory categoryOne = new SourceListCategory("categoria 1");
        final SourceListCategory categoryTwo = new SourceListCategory("cat 2");
        SourceListCategory categoryThree = new SourceListCategory("prova 3");

        SourceListItem itemA = new SourceListItem("SourceListItem A");
        SourceListItem itemB = new SourceListItem("SourceListItem B");

        SourceListItem itemC = new SourceListItem("SourceListItem D");
        SourceListItem itemD = new SourceListItem("SourceListItem E");
        SourceListItem itemE = new SourceListItem("Really really reallyeListItem");

        final SourceListItem itemG = new SourceListItem("SourceListItem G");
        SourceListItem itemH = new SourceListItem("SourceListItem H");
        SourceListItem itemI = new SourceListItem("SourceListItem I");

        

        final SourceListModel model = new SourceListModel();
        model.addCategory(categoryOne);
        model.addItemToCategory(itemA, categoryOne);
        model.addItemToCategory(itemB, categoryOne);

        model.addCategory(categoryTwo);
        model.addItemToCategory(itemC, categoryTwo);
    

        model.addCategory(categoryThree);
        model.addItemToCategory(itemG, categoryThree);
    

        fSourceList = new SourceList(model);
//        sourceList.setFocusable(false);

        SourceListControlBar controlBar = new SourceListControlBar();
        controlBar.createAndAddButton(MacIcons.PLUS, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model.addItemToItem(new SourceListItem(("Hello")), itemG);
            }
        });
        controlBar.createAndAddButton(MacIcons.MINUS, null);
        controlBar.createAndAddPopdownButton(MacIcons.GEAR,
                new PopupMenuCustomizerUsingStrings(null, "Item One", "Item Two", "Item Three"));

        fSourceList.installSourceListControlBar(controlBar);

        JTextArea textArea = new JTextArea();

        JSplitPane splitPane = MacWidgetFactory.createSplitPaneForSourceList(fSourceList, textArea);

        // TODO make SourceList a better size by default
        splitPane.setDividerLocation(200);

        controlBar.installDraggableWidgetOnSplitPane(splitPane);

        return splitPane;
    }

    public UnifiedToolBar getUnifiedToolBar() {
        return fUnifiedToolBar;
    }

    public SourceList getSourceList() {
        return fSourceList;
    }

    

}
